﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Globalization;
using System.Web.UI.HtmlControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
	public partial class PS_HisHealthIssueInfo : System.Web.UI.UserControl
	{
		private IHealthBLL iHealthBLL = new HealthBLL();
		private Main main = new Main();
		private SsMng ssMng = new SsMng();
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);

			if (!IsUserControlPostBack)
			{
				this.ViewState.Add("IsUserControlPostBack", true);

				//if (Request.QueryString["sta"] != null)
				//    this.lblSta.Text = Request.QueryString["sta"].ToString();
                IssueTypeSelected = "UnderlyingDisease";
                
				lblEmpSeq.Text = Session["emp_seq"].ToString();
				LoadDataGrid();
                
			}
		}

		protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
		{
			if (e.CommandName == "Delete")
			{
				int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
			}

			LoadDataGrid();
		}
		
		public void LoadDataGrid()
		{
			var dataSource = iHealthBLL.ListHealthByEmpSeq(Convert.ToString(SelectedEmployeeSeq), rblIssueType.SelectedValue); 
			if (dataSource == null)
				SetRadGrid(new DataTable());
			else
				SetRadGrid(dataSource);
		}

		private void SetRadGrid(object dataSource)
		{
            RadGridHealthIssue.DataSource = dataSource;
			foreach (GridColumn col in RadGridHealthIssue.MasterTableView.Columns)
			{
				if (col.UniqueName == "Description")
				{

					switch (SelectedIssueType)
					{
						case "Allergic":
							col.HeaderText = "ชื่อยาที่แพ้";
							break;
						case "PersonalMedicine":
                            
							col.HeaderText = "ชื่อยาที่ใช้ประจำ";
							break;
						default: // UnderlyingDisease
							col.HeaderText = "ชื่อโรคประจำตัว";
							break;
					}
				}
			}

			foreach (GridDataItem item1 in RadGridHealthIssue.MasterTableView.Items)
			{
				item1.Edit = false;
			}
			RadGridHealthIssue.Rebind(); 
 		} 

		protected void RadGridHealth_ItemCommand(object sender, GridCommandEventArgs e)
		{ 
			if (e.CommandName == RadGrid.InitInsertCommandName)
			{
				e.Canceled = true;
				System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
			    //rblIssueType.Visible = false;

				newValues["Sequence"] = -1; 
				newValues["IssueType"] = SelectedIssueType;
				newValues["EmployeeSequence"] = lblEmpSeq.Text;
				e.Item.OwnerTableView.InsertItem(newValues);
			} else if (e.CommandName == "Delete")
			{
				var item = ((GridDataItem)e.Item);
				var sequenceStr = item["Sequence"].Text;
				var sequenceInt = Convert.ToInt32(sequenceStr);
				iHealthBLL.Delete(sequenceInt, ssMng.EmpSeqI);
			}
            
            LoadDataGrid();
		}

		protected void RadGridHealth_ItemCreated(object sender, GridItemEventArgs e)
		{

		}

		protected void RadGridHealth_ItemDataBound(object sender, GridItemEventArgs e)
		{

		}
        protected void rblIssueType_SelectedIndexChanged(object sender, EventArgs e)
		{
		    LoadDataGrid();
		}


		protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
		{
			if (this.lblSta.Text == "v")
			{
				//RadGrid1.Columns[0].Visible = false;
				//RadGrid1.Columns[1].Visible = false;
			} 
		}
		#region Properties
		private int SelectedEmployeeSeq
		{
			get { return Convert.ToInt32(lblEmpSeq.Text); }
		}
		public string SelectedIssueType
		{
			get { return rblIssueType.SelectedValue; }
		}
		protected bool IsUserControlPostBack
		{
			get
			{
				return ViewState["IsUserControlPostBack"] != null;
			}
		}

		private string IssueTypeSelected
		{
			get
			{
				return Session["issue_type"].ToString();
			}
			set
			{
				Session["issue_type"] = value;
				rblIssueType.SelectedValue = value;
			}
		}

		#endregion Properties
		

	}
}